var UILoading, ns, ajpush, count = 0,
    toast;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            Username: '',
            Password: '',
            action: '',
            device_type: '',
            SystemNumber: '',
            device_id: ''
        },
        companyName: '',
        isSingleSite: CONFIG.isSingleSite,
        logoImagePath: '../icon/login_bg_font.png',
        // logoImagePath: '../icon/login_logo.png',
        hasSysNumber: false
    },
    methods: {
        get_data: function () {
            var that = this;
            that.companyName = api.getPrefs({
                sync: true,
                key: 'companyName'
            });
            that.check_switch_status();
            return;
            ns.post({
                action: 'getapplogo'
            }, function (succeed, data, err) {
                if (succeed) {
                    if (data.logoImagePath) {
                        that.logoImagePath = data.logoImagePath;
                    }
                }
            }, {
                ignoreuid: true
            });
        },
        check_switch_status: function () {
            var that = this;
            that.hasSysNumber = false;
            if (!that.isSingleSite) {
                var apiOption = ns.getAPIUrl();
                if (apiOption) {
                    that.hasSysNumber = true;
                }
            } else {
                ns.removeAPIUrl();
            }
        },
        do_switch: function () {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '切换公司后需要重新输入系统编号, 确认切换?',
                buttons: ['确认', '取消']
            }, function (ret, err) {
                var ServiceStatus = -1;
                if (ret.buttonIndex == 1) {
                    ns.removeAPIUrl();
                    that.companyName = '';
                    that.check_switch_status();
                }
            });
        },
        get_api: function () {
            var that = this;
            if (that.isSingleSite || that.hasSysNumber) {
                that.do_login();
                return;
            }
            if (that.form.SystemNumber == '') {
                api.toast({
                    msg: '请输入系统编号',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Username == '') {
                api.toast({
                    msg: '请输入用户名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.Password == '') {
                api.toast({
                    msg: '请输入密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });
            that.form.action = "getapi";
            ns.post(that.form, function (succeed, data, err) {
                if (succeed) {
                    ns.setAPIUrl(data.apiurl, data.companyname);
                    setTimeout(function () {
                        that.do_login();
                    }, 200)
                } else if (err) {
                    toast.hide();
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                ignoreuid: true
            });
        },
        do_login: function () {
            var that = this;
            if (!that.form.Username) {
                toast.hide();
                api.toast({
                    msg: '请输入用户名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.Password) {
                toast.hide();
                api.toast({
                    msg: '请输入密码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            // if (that.form.device_id == '' && count <= 1) {
            //     that.get_jiguang_register_id();
            //     count++;
            //     return;
            // }
            if (that.form.device_id == '') {
                api.toast({
                    msg: '极光id获取失败',
                    duration: 1000,
                    location: 'bottom'
                });
            }
            that.form.action = "login";
            console.log(JSON.stringify(that.form))
            ns.post(that.form, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    //是否绑定员工（大于零绑定了员工）
                    api.setPrefs({
                        key: 'StaffID',
                        value: data.StaffID
                    });
                    api.setPrefs({
                        key: 'StaffName',
                        value: data.StaffName
                    });
                    if (data.companyname) {
                        api.setPrefs({
                            key: 'companyName',
                            value: data.companyname
                        });
                    }
                    if (data.uid) {
                        api.setPrefs({
                            key: 'uid',
                            value: data.uid
                        });
                    }
                    if (that.form.Username) {
                        api.setPrefs({
                            key: 'loginname',
                            value: that.form.Username
                        });
                    }
                    if (data.username) {
                        api.setPrefs({
                            key: 'username',
                            value: data.username
                        });
                    }
                    if (data.headimg) {
                        api.setPrefs({
                            key: 'headimg',
                            value: data.headimg
                        });
                    }
                    if (data.phonenumber) {
                        api.setPrefs({
                            key: 'phonenumber',
                            value: data.phonenumber
                        });
                    }
                    if (data.positionname) {
                        api.setPrefs({
                            key: 'positionname',
                            value: data.positionname
                        });
                    }

                    api.sendEvent({
                        name: 'onlogin'
                    });
                } else if (err) {
                    if (!that.hasSysNumber) {
                        ns.removeAPIUrl();
                    }
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                ignoreuid: true
            });
        },
        get_jiguang_register_id: function () {
            var that = this;
            ajpush.getRegistrationId(function (res, err) {
                console.log(JSON.stringify(res));
                if (res && res.id) {
                    that.form.device_id = res.id;
                    api.setPrefs({
                        key: 'jpush_device_id',
                        value: res.id
                    });
                }
            });
        },
        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
        }
    }
});

apiready = function () {
    UILoading = api.require('UILoading');
    ns = window.Foresight.Util;
    ajpush = api.require('ajpush');
    toast = new auiToast();
    app.set_status_bar();
    app.get_jiguang_register_id();
    app.form.device_type = api.systemType;
    app.get_data();
}
